package weaver.jobs.xfjt.duss.utils;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-08-30
 * @Description: 定时任务工具类
 * @ModifyLog:
 **/

public class XfjtUtil {
   /**
   *@Description: 判断当前日期是否属于参数日期范围
   *@Param: [begDate, endDate]
   *@return: boolean
   *@Author: Dusongsong
   *@Date: 2022/8/30
   */
    public static boolean isWithinRange(String begDate,String endDate){
        String currentDate = DateUtil.getCurrentDate();
        Date curdate = DateUtil.parseToDate(currentDate);
        Date begdate = DateUtil.parseToDate(begDate);
        Date enddate = DateUtil.parseToDate(endDate);
        return !(curdate.before( begdate )|| curdate.after( enddate ));
    }

    /**
    *@Description: 间隔天数发起
    *@Param: [days]
    *@return: java.lang.String
    *@Author: Dusongsong
    *@Date: 2022/8/30
    */
    public static boolean runRntervalDay(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgts = Util.getIntValue(requestMainMap.get( "jgts" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        int newDays =  DateUtil.dayDiff(currentDate,dcrq);
        new BaseBean().writeLog(">>>>>runRntervalDay>>>>>newDays="+newDays);
        if(newDays%jgts==0){
            String requestid = createWorkflow(zrr,currentDate,requestMainMap);
            if(Util.getIntValue( requestid )>0){
                RecordSet rs = new RecordSet(  );
                String sql = "insert into uf_zqdc_dt1(mainid,fklc,fkfqrq,xywcfkrq,sfwcfk) values(?,?,?,?,?)";
                flag =  rs.executeUpdate(sql, dataid,requestid,currentDate,xywcfkrq,1);
            }
        }

        return flag;
    }

    /**
     *@Description: 间隔天数发起
     *@Param: [days]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runTaskRntervalDay(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String fqrq = Util.null2String(requestMainMap.get( "fqrq" ));
        int jgts = Util.getIntValue(requestMainMap.get( "jgts" ));
        String [] rwzxr = Util.null2String(requestMainMap.get( "rwzxr" )).split(",");
        String xywcfkrq = Util.null2String(requestMainMap.get( "yqwcrq" ));
        int newDays =  DateUtil.dayDiff(currentDate,fqrq);
        new BaseBean().writeLog(">>>>>runTaskRntervalDay>>>>>newDays="+newDays);
        if(newDays%jgts==0){
            for (String userid:rwzxr) {
                requestMainMap.put("fkr", userid );
                String requestid = createTaskWorkflow(userid,currentDate,requestMainMap);
                if(Util.getIntValue( requestid )>0){
                    RecordSet rs = new RecordSet(  );
                    String sql = "insert into uf_zqrwtz_dt1(mainid,fklc,fkr,rwksrq,yqwcrq,sfwcfk) values(?,?,?,?,?,?)";
                    flag =  rs.executeUpdate(sql, dataid,requestid,userid,currentDate,xywcfkrq,1);
                }
            }
        }

        return flag;
    }
    /**
     *@Description: 间隔天数发起
     *@Param: [days]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runProjectRntervalDay(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgts = Util.getIntValue(requestMainMap.get( "jgts" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        int newDays =  DateUtil.dayDiff(currentDate,dcrq);
        new BaseBean().writeLog(">>>>>runProjectRntervalDay>>>>>newDays="+newDays);
        if(newDays%jgts==0){
            String requestid = createProjectWorkflow(zrr,currentDate,requestMainMap);
            if(Util.getIntValue( requestid )>0){
                RecordSet rs = new RecordSet(  );
                String sql = "insert into uf_zdgcdc_dt1(mainid,fklc,fkfqrq,xywcfkrq,sfwcfk) values(?,?,?,?,?)";
                flag =  rs.executeUpdate(sql, dataid,requestid,currentDate,xywcfkrq,1);
            }
        }

        return flag;
    }

    /**
     *@Description: 间隔周数发起
     *@Param: [weeks,weekday]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runRntervalWeek(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgzs = Util.getIntValue(requestMainMap.get( "jgzs" ));
        int mzjfqfk = Util.getIntValue(requestMainMap.get( "mzjfqfk" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        Date curdate = DateUtil.parseToDate(currentDate);
        int curweek = DateUtil.getWeekOfYear(curdate);
        Date governdate = DateUtil.parseToDate(dcrq);
        int governweek = DateUtil.getWeekOfYear(governdate);
        new BaseBean().writeLog(">>>>>runRntervalWeek>>>>>governweek="+governweek);
        if((curweek-governweek)%jgzs==0) {
            int newweekday = DateUtil.getWeek( currentDate );
            if (newweekday == mzjfqfk) {
               String requestid = createWorkflow(zrr,currentDate,requestMainMap);
                if(Util.getIntValue( requestid )>0){
                    RecordSet rs = new RecordSet(  );
                    String sql = "insert into uf_zqdc_dt1(mainid,fklc,fkfqrq,xywcfkrq,sfwcfk) values(?,?,?,?,?)";
                    flag =  rs.executeUpdate(sql,dataid,requestid,currentDate,xywcfkrq,1);
                }
            }
        }
        return flag;
    }
    /**  /**
     *@Description: 间隔周数发起
     *@Param: [weeks,weekday]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runTaskRntervalWeek(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String fqrq = Util.null2String(requestMainMap.get( "fqrq" ));
        int jgzs = Util.getIntValue(requestMainMap.get( "jgzs" ));
        int mzjcjrw = Util.getIntValue(requestMainMap.get( "mzjcjrw" ));
        String [] rwzxr = Util.null2String(requestMainMap.get( "rwzxr" )).split(",");
        String xywcfkrq = Util.null2String(requestMainMap.get( "yqwcrq" ));
        Date curdate = DateUtil.parseToDate(currentDate);
        int curweek = DateUtil.getWeekOfYear(curdate);
        Date governdate = DateUtil.parseToDate(fqrq);
        int governweek = DateUtil.getWeekOfYear(governdate);
        new BaseBean().writeLog(">>>>>runTaskRntervalWeek>>>>>governweek="+governweek);
        if((curweek-governweek)%jgzs==0) {
            int newweekday = DateUtil.getWeek( currentDate );
            if (newweekday == mzjcjrw) {
                for (String userid:rwzxr) {
                    requestMainMap.put("fkr", userid );
                    String requestid = createTaskWorkflow(userid, currentDate, requestMainMap);
                    if (Util.getIntValue(requestid) > 0) {
                        RecordSet rs = new RecordSet();
                        String sql = "insert into uf_zqrwtz_dt1(mainid,fklc,fkr,rwksrq,yqwcrq,sfwcfk) values(?,?,?,?,?,?)";
                        flag = rs.executeUpdate(sql, dataid, requestid, userid,currentDate, xywcfkrq,1);
                    }
                }
            }
        }
        return flag;
    }
    /**
     *@Description: 间隔周数发起
     *@Param: [weeks,weekday]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runProjectRntervalWeek(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgzs = Util.getIntValue(requestMainMap.get( "jgzs" ));
        int mzjfqfk = Util.getIntValue(requestMainMap.get( "mzjfqfk" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        Date curdate = DateUtil.parseToDate(currentDate);
        int curweek = DateUtil.getWeekOfYear(curdate);
        Date governdate = DateUtil.parseToDate(dcrq);
        int governweek = DateUtil.getWeekOfYear(governdate);
        new BaseBean().writeLog(">>>>>runProjectRntervalWeek>>>>>governweek="+governweek);
        if((curweek-governweek)%jgzs==0) {
            int newweekday = DateUtil.getWeek( currentDate );
            if (newweekday == mzjfqfk) {
                String requestid = createProjectWorkflow(zrr,currentDate,requestMainMap);
                if(Util.getIntValue( requestid )>0){
                    RecordSet rs = new RecordSet(  );
                    String sql = "insert into uf_zdgcdc_dt1(mainid,fklc,fkfqrq,xywcfkrq,sfwcfk) values(?,?,?,?,?)";
                    flag =  rs.executeUpdate(sql, dataid,requestid,currentDate,xywcfkrq,1);
                }
            }
        }
        return flag;
    }

    /**
    *@Description: 间隔月数发起
    *@Param: [months,date]
    *@return: java.lang.String
    *@Author: Dusongsong
    *@Date: 2022/8/30
    */
    public static boolean runRntervalMonth(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgys = Util.getIntValue(requestMainMap.get( "jgys" ));
        int mydjtfqfk = Util.getIntValue(requestMainMap.get( "mydjtfqfk" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        int curMonth = Util.getIntValue(DateUtil.getMonth());
        Calendar calendar = DateUtil.getCalendar(dcrq);
        int governMonth = Util.getIntValue(DateUtil.getMonth( calendar ));
        new BaseBean().writeLog(">>>>>runRntervalMonth>>>>>governMonth="+governMonth);
        if((curMonth-governMonth)%jgys==0) {
            int newdate = Util.getIntValue(currentDate.substring( 8,10 ));
            if(newdate == mydjtfqfk){
                String requestid = createWorkflow(zrr,currentDate,requestMainMap);
                if(Util.getIntValue( requestid )>0){
                    RecordSet rs = new RecordSet(  );
                    String sql = "insert into uf_zqdc_dt1(mainid,fklc,fkfqrq,xywcfkrq,sfwcfk) values(?,?,?,?,?)";
                    flag = rs.executeUpdate(sql,dataid,requestid,currentDate,xywcfkrq,1);
                }
            }
        }
        return flag;
    }
    /**
     *@Description: 间隔月数发起
     *@Param: [months,date]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runTaskRntervalMonth(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String fqrq = Util.null2String(requestMainMap.get( "fqrq" ));
        int jgys = Util.getIntValue(requestMainMap.get( "jgys" ));
        int mydjtcjrw = Util.getIntValue(requestMainMap.get( "mydjtcjrw" ));
        String [] rwzxr = Util.null2String(requestMainMap.get( "rwzxr" )).split(",");
        String xywcfkrq = Util.null2String(requestMainMap.get( "yqwcrq" ));
        int curMonth = Util.getIntValue(DateUtil.getMonth());
        Calendar calendar = DateUtil.getCalendar(fqrq);
        int governMonth = Util.getIntValue(DateUtil.getMonth( calendar ));
        new BaseBean().writeLog(">>>>>runTaskRntervalMonth>>>>>governMonth="+governMonth);
        if((curMonth-governMonth)%jgys==0) {
            int newdate = Util.getIntValue(currentDate.substring( 8,10 ));
            if(newdate == mydjtcjrw){
                for (String userid:rwzxr) {
                    requestMainMap.put("fkr", userid );
                    String requestid = createTaskWorkflow(userid, currentDate, requestMainMap);
                    if (Util.getIntValue(requestid) > 0) {
                        RecordSet rs = new RecordSet();
                        String sql = "insert into uf_zqrwtz_dt1(mainid,fklc,fkr,rwksrq,yqwcrq,sfwcfk) values(?,?,?,?,?,?)";
                        flag = rs.executeUpdate(sql, dataid, requestid, userid,currentDate, xywcfkrq,1);
                    }
                }
            }
        }
        return flag;
    }
    /**
     *@Description: 间隔月数发起
     *@Param: [months,date]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    public static boolean runProjectRntervalMonth(String currentDate,Map<String, String> requestMainMap,String dataid){
        boolean flag = false;
        String dcrq = Util.null2String(requestMainMap.get( "dcrq" ));
        int jgys = Util.getIntValue(requestMainMap.get( "jgys" ));
        int mydjtfqfk = Util.getIntValue(requestMainMap.get( "mydjtfqfk" ));
        String zrr = Util.null2String(requestMainMap.get( "zrr" ));
        String xywcfkrq = Util.null2String(requestMainMap.get( "xywcfkrq" ));
        int curMonth = Util.getIntValue(DateUtil.getMonth());
        Calendar calendar = DateUtil.getCalendar(dcrq);
        int governMonth = Util.getIntValue(DateUtil.getMonth( calendar ));
        new BaseBean().writeLog(">>>>>runProjectRntervalMonth>>>>>governMonth="+governMonth);
        if((curMonth-governMonth)%jgys==0) {
            int newdate = Util.getIntValue(currentDate.substring( 8,10 ));
            if(newdate == mydjtfqfk){
                String requestid = createProjectWorkflow(zrr,currentDate,requestMainMap);
                if(Util.getIntValue( requestid )>0){
                    RecordSet rs = new RecordSet(  );
                    String sql = "insert into uf_zdgcdc_dt1(mainid,fklc,fkfqrq,xywcfkrq,sfwcfk) values(?,?,?,?,?)";
                    flag =  rs.executeUpdate(sql, dataid,requestid,currentDate,xywcfkrq,1);
                }
            }
        }
        return flag;
    }

    /**
    *@Description: 创建流程
    *@Param: [userid, curdate, requestMainMap]
    *@return: java.lang.String
    *@Author: Dusongsong
    *@Date: 2022/8/30
    */
    private static String createWorkflow(String userid,String curdate, Map<String, String> requestMainMap){
        String workflowId = "56";
        String workflowName = "周期督查反馈";
        String isNextFlow = "1";
        String requestid = "";
        try {
            ResourceComInfo rci = new ResourceComInfo();
            String requestName = workflowName + "-" + rci.getLastname(userid) + "-" + curdate;
            WorkflowCreateHandler handler = new WorkflowCreateHandler();
            requestid = handler.WorkflowCreateByRequestMap( userid, workflowId, requestName, isNextFlow, requestMainMap );
        }catch (Exception e){
            e.printStackTrace();
        }
        return requestid;
    }
    /**
     *@Description: 创建流程
     *@Param: [userid, curdate, requestMainMap]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    private static String createTaskWorkflow(String userid,String curdate, Map<String, String> requestMainMap){
        String workflowId = "75";
        String workflowName = "周期任务反馈";
        String isNextFlow = "1";
        String requestid = "-1";
        try {
            ResourceComInfo rci = new ResourceComInfo();
            String requestName = workflowName + "-" + rci.getLastname(userid) + "-" + curdate;
            WorkflowCreateHandler handler = new WorkflowCreateHandler();
            requestid = handler.WorkflowCreateByRequestMap( userid, workflowId, requestName, isNextFlow, requestMainMap );
        }catch (Exception e){
            e.printStackTrace();
        }
        return requestid;
    }
    /**
     *@Description: 创建流程
     *@Param: [userid, curdate, requestMainMap]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/8/30
     */
    private static String createProjectWorkflow(String userid,String curdate, Map<String, String> requestMainMap){
        String workflowId = "58";
        String workflowName = "重点工程建设督查反馈";
        String isNextFlow = "1";
        String requestid = "";
        try {
            ResourceComInfo rci = new ResourceComInfo();
            String requestName = workflowName + "-" + rci.getLastname(userid) + "-" + curdate;
            WorkflowCreateHandler handler = new WorkflowCreateHandler();
            requestid = handler.WorkflowCreateByRequestMap( userid, workflowId, requestName, isNextFlow, requestMainMap );
        }catch (Exception e){
            e.printStackTrace();
        }
        return requestid;
    }

    /**
    *@Description: 获取流程表单字段
    *@Param: [formid, tablename]
    *@return: java.lang.String
    *@Author: Dusongsong
    *@Date: 2022/8/30
    */
    public static String  getFieldName(String formid){
        RecordSet rs = new RecordSet(  );
        String strs = "";
        rs.executeQuery( "select fieldname from workflow_billfield where billid=? and viewtype=0", formid );
        while (rs.next()) {
            String fieldname = Util.null2String( rs.getString( "fieldname" ) );
            strs +="".equals( fieldname )?fieldname:","+fieldname;
        }
        return strs;
    }
}
